package com.egene.zh.activity.settings;

import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.egene.zh.R;
import com.egene.zh.base.BaseActivity;
import com.egene.zh.dao.UserDao;
import com.egene.zh.model.User;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@EActivity(R.layout.activity_account)
public class AccountActivity extends BaseActivity {
    //ToolBar Views
    @ViewById(R.id.actionBar)
    Toolbar mToolbarView;
    @ViewById(R.id.toolbar_title)
    TextView mToolBarTitle;


    @ViewById
    TextView tvName;
    @ViewById
    TextView tvSex;
    @ViewById
    TextView tvAge;
    @ViewById
    TextView tvWeight;
    @ViewById
    TextView tvHeight;


    @Bean
    UserDao userDao;

    @AfterViews
    void initViews() {
        initToolBar();
        User self = userDao.getUser();
        tvName.setText(self.getLoginID());
        if (self.getSex().equals("" + User.FEMALE)) {
            tvSex.setText(R.string.female);
        } else if (self.getSex().equals("" + User.MALE)) {
            tvSex.setText(R.string.male);
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date birthDay = null;
        try {
            birthDay = simpleDateFormat.parse(self.getBirthdayString());
//            birthDay = simpleDateFormat.parse("2011-02-02");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (birthDay != null) {
            Date today = new Date(System.currentTimeMillis());
            int y1 = today.getYear();
            int y2 = birthDay.getYear();

            int age = y1 - y2;
            tvAge.setText(age > 0 ? "" + age : "0" );
        }

//        tvAge.setText(self.get());
        tvWeight.setText(String.format("%s kg", self.getWeight()));
        tvHeight.setText(String.format("%s cm", self.getHeight()));
    }

    private void initToolBar() {
        mToolBarTitle.setText(R.string.personal_detail);
        mToolbarView.setNavigationIcon(R.drawable.ic_back);
        mToolbarView.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


}
